package com.gitee.hermer.engine.jars.dependent.redis.configuration;

import javax.sql.DataSource;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.ImportResource;
import org.springframework.data.redis.connection.jedis.JedisConnectionFactory;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.transaction.PlatformTransactionManager;
import org.springframework.transaction.annotation.EnableTransactionManagement;
import org.springframework.transaction.jta.JtaTransactionManager;

import com.gitee.hermer.boot.jee.commons.log.UtilsContext;
import com.gitee.hermer.engine.jars.dependent.dsm.IEnginePropertiesAware;
import com.gitee.hermer.engine.jars.dependent.redis.config.RedisProperties;

import redis.clients.jedis.JedisPoolConfig;


@Configuration
public class RedisAutoConfiguration implements IEnginePropertiesAware<RedisProperties>{

	private RedisProperties properties;
	
	
	@Bean
	public RedisProperties redisProperties(){
		return new RedisProperties();
	}
	
	@Bean
	public JedisPoolConfig jedisPoolConfig()
	{
		JedisPoolConfig jedisPoolConfig=new JedisPoolConfig();
		jedisPoolConfig.setMaxIdle(properties.getMaxIdle());
		jedisPoolConfig.setMaxTotal(properties.getMaxTotal());
		jedisPoolConfig.setMaxWaitMillis(properties.getMaxWaitMillis());
		jedisPoolConfig.setTimeBetweenEvictionRunsMillis(properties.getTimeBetweenEvictionRunsMillis());
		jedisPoolConfig.setMinEvictableIdleTimeMillis(properties.getMinEvictableIdleTimeMillis());
		jedisPoolConfig.setTestOnBorrow(properties.getTestOnBorrow());
		jedisPoolConfig.setMinIdle(properties.getMinIdle());
		return jedisPoolConfig;
	}
	
	@Bean
	@Autowired
	public JedisConnectionFactory jedisConnectionFactory(JedisPoolConfig poolConfig){

		JedisConnectionFactory jedisConnectionFactory = new JedisConnectionFactory();
		jedisConnectionFactory.setHostName(properties.getHost());
		jedisConnectionFactory.setPort(properties.getPort());
		jedisConnectionFactory.setPassword(properties.getPassword());
		jedisConnectionFactory.setPoolConfig(poolConfig);
		return jedisConnectionFactory;
	}
	
	@Bean
	@Autowired
	public StringRedisTemplate stringRedisTemplate(JedisConnectionFactory connectionFactory){
		StringRedisTemplate stringRedisTemplate = new StringRedisTemplate();
		stringRedisTemplate.setEnableTransactionSupport(true);
		stringRedisTemplate.setConnectionFactory(connectionFactory);
		return stringRedisTemplate;
	}
	
	@Bean
	@Autowired
	public StringRedisTemplate stringRedisTemplateNoTransaction(JedisConnectionFactory connectionFactory){
		StringRedisTemplate stringRedisTemplate = new StringRedisTemplate();
		stringRedisTemplate.setEnableTransactionSupport(false);
		stringRedisTemplate.setConnectionFactory(connectionFactory);
		return stringRedisTemplate;
	}
	

	@Override
	@Autowired
	public void setProperties(RedisProperties properties) {
		this.properties = properties;
	}


}
